package com.lwb.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lwb.commonutils.JwtUtils;
import com.lwb.commonutils.MD5;
import com.lwb.educenter.entity.UcenterMember;
import com.lwb.educenter.entity.vo.RegisterVo;
import com.lwb.educenter.mapper.UcenterMemberMapper;
import com.lwb.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwb.serviceBase.StudyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-12-03
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    //登录
    @Override
    public String login(UcenterMember ucenterMember) {
        //获取登录的手机号和密码
        String mobile = ucenterMember.getMobile();
        String password = ucenterMember.getPassword();
        //手机号和密码的非空判断
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new StudyException(20001,"登陆失败，手机号或密码错误！！");
        }
        //判断手机号是否正确
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        UcenterMember member = baseMapper.selectOne(queryWrapper);
        if(member == null){
            throw new StudyException(20001,"登陆失败，手机号未注册！！");
        }

        //判断密码是否正确
        //存储数据库中的密码是加密的，再和我们数据库的密码进行比较
        //加密方式 md5
        if (!MD5.encrypt(password).equals(member.getPassword())){
            throw new StudyException(20001,"登陆失败，密码错误！！");
        }

        //判断账号是否被禁用
        if(member.getIsDisabled()){
            throw new StudyException(20001,"登陆失败，账号被禁用，如有疑问，请联系管理员！！");
        }

        //登陆成功
        //生成token字符串，使用jwt工具类
        String jwtToken = JwtUtils.getJwtToken(member.getId(), member.getNickname());
        return jwtToken;
    }

    //注册方法
    @Override
    public void register(RegisterVo registerVo) {
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();
        //非空判断
        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(mobile) || StringUtils.isEmpty(nickname) || StringUtils.isEmpty(password)){
            throw new StudyException(20001,"注册失败！，请填写完整信息");
        }
        //判断验证码
        //获取redis的验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if(!code.equals(redisCode)){
            throw new StudyException(20001,"注册失败！，验证码错误！！");
        }

        //判断手机号在数据库中是否有重复
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(queryWrapper);
        if(count>0){
            throw new StudyException(20001,"注册失败！，手机号已经被注册！！");
        }

        //数据添加
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setMobile(mobile);
        ucenterMember.setNickname(nickname);
        ucenterMember.setPassword(MD5.encrypt(password));
        ucenterMember.setIsDisabled(false);
        ucenterMember.setAvatar("");
        baseMapper.insert(ucenterMember);
    }

    @Override
    public UcenterMember getMemberByOpenId(String openid) {
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("openid",openid);
        UcenterMember ucenterMember = baseMapper.selectOne(queryWrapper);

        return ucenterMember;
    }

    @Override
    public Integer countRegister(String day) {
        Integer count = baseMapper.countRegister(day);
        return count;
    }
}
